﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common;
using log4net;
using SqlSugar;
using AutoFacUtils;
using CashFlow.Web.Def;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.Db.Repository;
using CashFlow.Web.Service;
using CashFlow.Web.Models.Fee.Result;

namespace CashFlow.Web.Service.Implement
{
    public class FeeService : MyService<FeeEntity>, IFeeService
    {
        private IFeeRepository GetFeeRepository() { return AutoFacHelper.Get<IFeeRepository>(); }

        public FeeStatisticsInfo QueryByUserId(int userId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<FeeEntity>()
                               .GroupBy(x => x.UserId)
                               .Where(x => x.UserId == userId)
                               .Select(x => new FeeStatisticsInfo
                               {
                                   UserId = x.UserId,
                                   OrderProfitFee = SqlFunc.AggregateSumNoNull(x.OrderProfitFee),
                                   ShopManagerFee = SqlFunc.AggregateSumNoNull(x.ShopManagerManageFee),
                                   BoundFee = SqlFunc.AggregateSumNoNull(x.BoundFee),
                                   ConsignmentSaleFee = SqlFunc.AggregateSumNoNull(x.ConsignmentSaleFee)
                               })
                               .First();
                return entity;
            }
        }

        public FeeEntity QueryByUserIdDate(int userId, string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var item = db.Queryable<FeeEntity>()
                             .Where(x => x.UserId == userId
                                && x.Date == date)
                             .First();
                return item;
            }
        }
    }
}
